package com.example.test2.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;

import com.example.test2.R;
import com.example.test2.databinding.ActivityNewsBinding;
import com.example.test2.db.entity.New;
import com.example.test2.http.bean.News;

// TODO: 2024/6/02 新闻
public class NewsActivity extends BaseActivity<ActivityNewsBinding> {

    @Override
    protected void initData() {
        boolean add = getIntent().getBooleanExtra("add", false);
        initContent(add);
    }

    private void initContent(boolean add) {
        if (!add) {
            News.Result.Data data = (News.Result.Data) getIntent().getSerializableExtra("data");
            if (data != null) {
                getBinding().rlMain.setVisibility(View.GONE);
                getBinding().titleBar.setTitle(data.getTitle());
                getBinding().webView.loadUrl(data.getUrl());
            }
        }else {
            New aNew = (New) getIntent().getSerializableExtra("data");
            if (aNew != null) {
                getBinding().titleBar.setTitle(aNew.getOneTitle());
                getBinding().webView.setVisibility(View.GONE);
                getBinding().rlMain.setVisibility(View.VISIBLE);
                String author_name = aNew.getAuthor();

                getBinding().tvContent.setText(aNew.getContent());
                getBinding().tvAuthorName.setText(author_name);
                getBinding().tvTime.setText(aNew.getDate());
                getBinding().tvOne.setText(aNew.getOneTitle());
                getBinding().tvTwo.setText(aNew.getTwoTitle());

                String path= aNew.getPath();
                if (!TextUtils.isEmpty(path)){
                    Bitmap bitmap= BitmapFactory.decodeFile(path);
                    getBinding().ivPic.setImageBitmap(bitmap);
                }
            }
        }
    }

    @Override
    protected void initClick() {
        super.initClick();
        getBinding().titleBar.setLeftIconOnClickListener(v -> {finish();});
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_news;
    }

    @Override
    protected ActivityNewsBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityNewsBinding.inflate(layoutInflater);
    }

}